package com.example.navigationdemo

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.example.navigationdemo.ui.theme.ComposeNewStudyTheme
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import kotlin.reflect.typeOf

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposeNewStudyTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    MainNavigation(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}

@Composable
fun MainNavigation(modifier: Modifier = Modifier) {
    val navController = rememberNavController()
    //默认是 SubGraph.Auth 这个组的路由
    NavHost(navController = navController, startDestination = SubGraph.Auth) {
        //分组路由 Auth
        navigation<SubGraph.Auth>(startDestination = Dest.AuthFirstScreen) {
            //分组路由 Auth组中的 AuthFirstScreen
            composable<Dest.AuthFirstScreen> {
                AuthFirst {
                    //跳转到 AuthSecondScreen 页面
                    navController.navigate(Dest.AuthSecondScreen)
                }
            }

            //分组路由 Auth组中的 AuthSecondScreen
            composable<Dest.AuthSecondScreen> {
                AuthSecond {
                    // TODO: 1
                    //跳转到 AuthSecondScreen 页面 并添加参数
                    //navController.navigate(Dest.DashSecondScreen(name = "xxxx", age = 20))

                    // TODO: 2
                    navController.navigate(
                        Dest.DashSecondScreen(
                            Dummy(
                                name = "json_jb_params",
                                age = 33
                            )
                        )
                    )
                }
            }
        }

        //分组路由 Dashboard
        navigation<SubGraph.Dashboard>(startDestination = Dest.DashFirstScreen) {
            //分组路由 Dest组中的 DashFirstScreen
            composable<Dest.DashFirstScreen> {
                DashboardFirst {

                }
            }
            // TODO: 1
            //分组路由 Dest组中的 DashSecondScreen
//            composable<Dest.DashSecondScreen> {
//                val dash = it.toRoute<Dest.DashSecondScreen>()
//                DashboardSecond(dash = dash) {
//
//                }
//            }

            // TODO: 2
            composable<Dest.DashSecondScreen>(
                typeMap = mapOf(typeOf<Dummy>() to DummyType)
            ) {
                val dash = it.toRoute<Dest.DashSecondScreen>()
                DashboardSecond(dash = dash) {

                }
            }
        }
    }
}

val DummyType = object : NavType<Dummy>(false) {
    override fun get(bundle: Bundle, key: String): Dummy? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            bundle.getParcelable(key, Dummy::class.java)
        } else {
            bundle.getParcelable(key)
        }
    }

    override fun parseValue(value: String): Dummy {
        return Json.decodeFromString(value)
    }

    override fun put(bundle: Bundle, key: String, value: Dummy) {
        bundle.putParcelable(key, value)
    }

    override fun serializeAsValue(value: Dummy): String {
        return Json.encodeToString(value)
    }

}


// TODO: 1
/**
 * 传递参数通过 DashSecondScreen 中的属性传递
 * navController.navigate(Dest.DashSecondScreen(name = "xxxx", age = 20))
 *
 * 参数解析,主要是  val dash = it.toRoute<Dest.DashSecondScreen>()
 * composable<Dest.DashSecondScreen> {
 *                 val dash = it.toRoute<Dest.DashSecondScreen>()
 *                 DashboardSecond(dash = dash) {
 *
 *                 }
 *             }
 *
 * 传递参数的获取 ，主要是 text = "Dashboard second screen \n name=${dash.name} age=${dash.age}",
 * @Composable
 * fun DashboardSecond(dash: Dest.DashSecondScreen, onClick: () -> Unit) {
 *     Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
 *         Text(
 *             text = "Dashboard second screen \n name=${dash.name} age=${dash.age}",
 *             modifier = Modifier.clickable { onClick.invoke() })
 *     }
 * }
 */


// TODO: 2
/**
 * 第二种方案主要是借助对象 json
 *
 * 定于 type
 *val DummyType = object : NavType<Dummy>(false) {
 *     override fun get(bundle: Bundle, key: String): Dummy? {
 *         return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 *             bundle.getParcelable(key, Dummy::class.java)
 *         } else {
 *             bundle.getParcelable(key)
 *         }
 *     }
 *
 *     override fun parseValue(value: String): Dummy {
 *         return Json.decodeFromString(value)
 *     }
 *
 *     override fun put(bundle: Bundle, key: String, value: Dummy) {
 *         bundle.putParcelable(key, value)
 *     }
 *
 *     override fun serializeAsValue(value: Dummy): String {
 *         return Json.encodeToString(value)
 *     }
 *
 * }
 *
 *
 * 导航传递参数
 * navController.navigate(
 *                         Dest.DashSecondScreen(
 *                             Dummy(
 *                                 name = "json_jb_params",
 *                                 age = 33
 *                             )
 *                         )
 *                     )
 *
 *
 *
 * 解析参数
 *
 *composable<Dest.DashSecondScreen>(
 *                 typeMap = mapOf(typeOf<Dummy>() to DummyType)
 *             ) {
 *                 val dash = it.toRoute<Dest.DashSecondScreen>()
 *                 DashboardSecond(dash = dash) {
 *
 *                 }
 *             }
 *
 *
 * 获取到值
 * @Composable
 * fun DashboardSecond(dash: Dest.DashSecondScreen, onClick: () -> Unit) {
 *     Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
 *         Text(
 *             // TODO: 2
 *             text = "Dashboard second screen \n name=${dash.dummy.name} age=${dash.dummy.age}",
 *             modifier = Modifier.clickable { onClick.invoke() })
 *     }
 * }
 * 
 *
 * 代码参考视频： https://www.youtube.com/watch?v=WFTzMx1pHCg
 */
